package com.example.maxboxtwo.components.activities;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.example.maxboxtwo.MainActivity;
import com.example.maxboxtwo.R;
import com.example.maxboxtwo.components.function.Compass.CompassView;
import com.example.maxboxtwo.components.function.Compass.GetCurrentPosition;
import com.example.maxboxtwo.components.function.Compass.LocationUtils;
import com.example.maxboxtwo.utils.listeners.CompassListener;

import org.w3c.dom.Text;

import static java.lang.Thread.sleep;

public class Compass extends AppCompatActivity {
    private SensorManager manager;
    private Sensor geomagnetic;
    private Sensor accelerate;
    private CompassView compassView;
    private CompassListener compassListener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_compass);
        compassView = findViewById(R.id.compass_inner);
        TextView orientation = findViewById(R.id.orientation);
        TextView information = findViewById(R.id.information);

        compassListener =  new CompassListener(compassView);
        manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        accelerate = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        geomagnetic = manager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

        // get position of location
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        GetCurrentPosition position = new GetCurrentPosition(locationManager,this,information);

       /* LocationUtils.register(this, 0, 0, new LocationUtils.OnLocationChangeListener() {
            @Override
            public void getLastKnownLocation(Location location) {
                Log.e("xyh", "onLocationChanged: " + location.getLatitude());
            }

            @Override
            public void onLocationChanged(Location location) {
                //位置信息变化时触发
                Log.e("xyh", "定位方式：" + location.getProvider());
                Log.e("xyh", "纬度：" + location.getLatitude());
                Log.e("xyh", "经度：" + location.getLongitude());
                Log.e("xyh", "海拔：" + location.getAltitude());
                Log.e("xyh", "时间：" + location.getTime());
                Log.e("xyh", "国家：" + LocationUtils.getCountryName(Compass.this, location.getLatitude(), location.getLongitude()));
                Log.e("xyh", "获取地理位置：" + LocationUtils.getAddress(Compass.this, location.getLatitude(), location.getLongitude()));
                Log.e("xyh", "所在地：" + LocationUtils.getLocality(Compass.this, location.getLatitude(), location.getLongitude()));
                Log.e("xyh", "所在街道：" + LocationUtils.getStreet(Compass.this, location.getLatitude(), location.getLongitude()));

            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                Log.i("status","the status changed");
            }
        });
*/

        new Thread(()->{
           for(int a=0;a<5;a++){
               try {
                   compassView.setDegree(-10*a);
                   sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               compassView.postInvalidate();
           }
        }).start();

    }


    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        manager.registerListener(compassListener,geomagnetic,Sensor.TYPE_MAGNETIC_FIELD);
        manager.registerListener(compassListener,accelerate,Sensor.TYPE_ACCELEROMETER);
    }

    @Override
    protected void onPause() {
        super.onPause();
        manager.unregisterListener(compassListener);
    }


}
